﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeamOPSchoolCup.Domain.POCO;
using TeamOPSchoolCup.Domain.DAL;
using TeamOPSchoolCup.Domain.POCO.Members;
using System.Web.Security;
namespace TeamOPSchoolCup.Domain.Services
{
  public class NSSFService
  {
    public static bool AddNSSFRep(NSSFRep nssfrep, NSSF nssf)
    {
      bool n;
      using (EFDbContext db = new EFDbContext())
      {
        n = AddNSSFRep(db, nssfrep, nssf);
      }
      return n;
    }
    public static bool AddNSSFRep(EFDbContext db, NSSFRep nssfrep, NSSF nssf)
    {
      bool b = true;
      List<NSSFRep> nssfreps = db.NSSFs.Single(n => n.Name == nssf.Name).NSSFReps.ToList<NSSFRep>();

      if (nssfreps.Count == 0)
      {
        db.NSSFs.Single(n => n.NSSFId == nssf.NSSFId).NSSFReps.Add(nssfrep);
      }
      else
      {
        foreach (NSSFRep nr in nssfreps)
        {
          Member mem = db.Members.Where(m => m.MemberId == nr.MemberId).First();
          MembershipUser user = Membership.GetUser(mem.UserId);
          if (!Roles.IsUserInRole(user.ProviderName, "Inactive"))
          {
            b = false;
          }
        }
        if (b)
        {
          db.NSSFs.Single(n => n.NSSFId == nssf.NSSFId).NSSFReps.Add(nssfrep);
        }

      }
      return (db.SaveChanges() > 0);
    }
    public static bool AddNSSF(NSSF nssf)
    {
      bool n;
      using (EFDbContext db = new EFDbContext())
      {
        n = AddNSSF(db, nssf);
      }
      return n;
    }

    public static bool AddNSSF(EFDbContext db, NSSF nssf)
    {
      db.NSSFs.Add(nssf);
      return (db.SaveChanges() > 0);
    }

    public static NSSF GetNSSF(string name)
    {
      NSSF n;
      using (EFDbContext db = new EFDbContext())
      {
        n = GetNSSF(db, name);
      }
      return n;
    }

    public static NSSF GetNSSF(EFDbContext db, string name)
    {
      return db.NSSFs.Where(x => x.Name.Equals(name)).FirstOrDefault();
    }

    public static NSSFRep GetNSSFRep(NSSF nssf)
    {
      NSSFRep n;
      using (EFDbContext db = new EFDbContext())
      {
        n = GetNSSFRep(db, nssf);
      }

      return n;
    }

    public static NSSFRep GetNSSFRep(EFDbContext db, NSSF nssf)
    {
      NSSFRep n = null; ;
      foreach (NSSFRep nrp in db.NSSFs.Single(ns => ns.Name == nssf.Name).NSSFReps.ToList<NSSFRep>())
      {
        Member mem = db.Members.Where(m => m.MemberId == nrp.MemberId).First();
        MembershipUser user = Membership.GetUser(mem.UserId);
        if (!Roles.IsUserInRole(user.ProviderName, "Inactive"))
        {
          n = MemberService.GetNSSFRep(nssf.NSSFReps.First().Member.Name);
        }
      }
      return n;
    }
    public static Region GetRegion(NSSF nssf)
    {
      return RegionService.GetRegion(nssf.RegionId);
    }

    public static Region GetRegion(EFDbContext db, NSSF nssf)
    {
      return RegionService.GetRegion(db, nssf.RegionId);
    }
    public static bool SetNSSFRep(NSSFRep nssfrep, NSSF nssf)
    {
      bool n;
      using (EFDbContext db = new EFDbContext())
      {
        n = SetNSSFRep(db, nssfrep, nssf);
      }
      return n;
    }
    public static bool SetNSSFRep(EFDbContext db, NSSFRep nssfrep, NSSF nssf)
    {

      foreach (NSSFRep nr in db.NSSFs.Single(n => n.Name == nssf.Name).NSSFReps.ToList<NSSFRep>())
      {
        Member mem = db.Members.Where(m => m.MemberId == nr.MemberId).First();
        MembershipUser user = Membership.GetUser(mem.UserId);
        if (!Roles.IsUserInRole(user.ProviderName, "Inactive"))
        {
          Roles.AddUserToRole(user.ProviderName, "Inactive");
        }
      }

      db.NSSFs.Single(n => n.NSSFId == nssf.NSSFId).NSSFReps.Add(nssfrep);
      return (db.SaveChanges() > 0);
    }


  }
}
